package com.example.dao;

import com.example.bean.UserBean;
import com.example.mapper.generator.TeacherPoMapper;
import com.example.mapper.generator.po.TeacherPo;
import com.example.mapper.generator.po.TeacherPoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TeacherDao {

    @Autowired
    TeacherPoMapper teacherPoMapper;

    public UserBean getLoginInfo(Long id, String password) {
        TeacherPoExample example = new TeacherPoExample();
        TeacherPoExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andPasswordEqualTo(password);
        List<TeacherPo> teacherPoList = teacherPoMapper.selectByExample(example);
        if (teacherPoList.size() != 1) {
            return null;
        } else {
            TeacherPo teacherPo = teacherPoList.get(0);
            return UserBean.builder().id(id).name(teacherPo.getName()).password(teacherPo.getPassword()).phone(teacherPo.getPhone()).email(teacherPo.getEmail()).identity("teacher").build();
        }
    }

    public Long register(String name, String password, String phone, String email) {
        TeacherPo po = TeacherPo.builder().name(name).password(password).phone(phone).email(email).build();
        teacherPoMapper.insert(po);
        return po.getId();
    }

    public TeacherPo getTeacherById(Long id){
        return teacherPoMapper.selectByPrimaryKey(id);
    }
}
